package
{
	import as3isolib.core.IIsoDisplayObject;
	import as3isolib.display.IsoSprite;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.geom.Pt;
	import as3isolib.utils.IsoDrawingUtil;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public class GMDrawPath implements IGameMode
	{      
		private var m_previewNode:IIsoDisplayObject;
		private var m_previewLink:IIsoDisplayObject = Global.getIsoLineSprite(Global.originVector, Global.originVector);
		
		private var m_startNode:PathNode;
		private var m_endNode:PathNode;
		
		public function GMDrawPath()
		{
			m_previewNode = Global.getPathNodeImage();
			Global.isoScene.addChild(m_previewNode);
		}
		
		public function onMouseDown(me:MouseEvent):void{
			
		}
		
		private function getCurrentNode(me:MouseEvent):PathNode{
			var currentPosition:Point = new Point(me.stageX, me.stageY);
			var isoPosition:PathNode = Global.pt2Node(Global.isoView.localToIso(currentPosition));
			var extrapolatedNode:PathNode = extrapolatePosition(isoPosition);
			var currentNode:PathNode;
			
			var managedNode:PathNode = Global.pathManager.findNode(extrapolatedNode.v3);
			if(managedNode != null){
				currentNode = managedNode;
				Global.pathManager.highlightNode(currentNode);
			} else {
				Global.pathManager.removeHighlight();
				currentNode = extrapolatedNode;
			}
			
			return currentNode;
		}
		
		public function onMouseUp(me:MouseEvent):void{
			var node:PathNode = getCurrentNode(me);
			
			if(m_startNode == null){
				m_startNode = node;
				Global.pathManager.addNode(m_startNode);
			} else if(m_endNode == null){
				if(m_startNode.subtract(node).length > 0){
					m_endNode = node;	
					Global.pathManager.addNode(m_endNode);
				}				
			}
			
			if(m_startNode != null && m_endNode != null){
				Global.pathManager.addLink(new PathLink(m_startNode, m_endNode));
				Global.pathManager.addLink(new PathLink(m_endNode, m_startNode));
				
				
				m_startNode = null;
				m_endNode = null;
			}
		}
		
		public function onMouseMove(me:MouseEvent):void{
			var node:PathNode = getCurrentNode(me); 
			
			if(m_startNode != null){
				var lineDimensions:Vector3D = node.v3.subtract(m_startNode.v3);
				var lineImage:IIsoDisplayObject = Global.getIsoLineSprite(m_startNode.v3, node.v3);
				lineImage.setSize(lineDimensions.x, lineDimensions.y, lineDimensions.z);
				lineImage.x = m_startNode.x;
				lineImage.y = m_startNode.y;
				lineImage.z = m_startNode.z;
				
				updatePreviewLinkImage(lineImage);
			}
			
			m_previewNode.x = node.x;
			m_previewNode.y = node.y;
			m_previewNode.z = node.z;
			
		}
		
		private function updatePreviewLinkImage(image:IIsoDisplayObject):void{
			
			if(m_previewLink.parent != null){
				m_previewLink.parent.removeChild(m_previewLink);
			}
			
			m_previewLink = image;
			Global.isoScene.addChild(m_previewLink);
		}
		
		// for now this just snaps
		private function extrapolatePosition(pos:PathNode):PathNode{
			var sqSize:int = Constants.SQUARE_SIZE;
			var snappedX:Number = Math.round(pos.x/sqSize)*sqSize;
			var snappedY:Number = Math.round(pos.y/sqSize)*sqSize;
			var snappedZ:Number = Math.round(pos.z/sqSize)*sqSize;
			
			return new PathNode(new Vector3D(snappedX, snappedY, snappedZ));
		}			
	}
}